#ifndef RTP_PARSE_PORT_MANAGER_H
#define RTP_PARSE_PORT_MANAGER_H

#include <string>
#include <vector>
#include <cstdint>
#include "uv.h"

struct SocketFlags{
	bool ipv6Only{ false };
	bool udpReusePort{ false };
};

class PortManager {
private:
	enum class Protocol: uint8_t {
		UDP = 1,
		TCP,
	};

private:
    struct PortRange {
		explicit PortRange(uint16_t numPorts, uint16_t minPort): ports(numPorts, false), minPort(minPort){}

		std::vector<bool> ports;
		uint16_t minPort{ 0u };
		uint16_t numUsedPorts{ 0u };
	};
public:
	static uv_handle_t* Bind(Protocol protocol, std::string& ip, uint16_t port, SocketFlags& flags);

    static uv_udp_t* BindUdp(std::string& ip, uint16_t port, SocketFlags& flags) {
		return reinterpret_cast<uv_udp_t*>(Bind(Protocol::UDP, ip, port, flags));
	}

	static void Unbind(uint64_t hash, uint16_t port);

	static uint8_t ConvertSocketFlags(SocketFlags& flags, Protocol protocol, int family);
};



#endif 
